﻿using HtmlAgilityPack;
using NewsCollection.Common.Service;
using NewsCollection.Common.UpdateGUI;
using NewsCollection.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewsCollection.ListWeb
{
    class _2SaoVn
    {
        ProcessDb pdb = new ProcessDb();
        UpdateButton updateBtn = new UpdateButton();
        public void Run(ListView lv, Button btnStart, Button btnStop, int manuId,string folder)
        {

            Load load = new Load(lv);
            List<CategoryModel> ListCate = new List<CategoryModel>();

         

            string urlGoc = "http://2sao.vn/";

            var web = new HtmlWeb();
            Random r = new Random();
            int time = r.Next(0, 10000);

            System.Threading.Thread.Sleep(time);
            var doc = web.Load(urlGoc);

            //  var item2= GetHtmlAjax(urlGoc, 5);

            //load subcate trong tung category rieng chu khong phai load 1 luot tai trang home load giong trang yanvn

            int dem = 0;
           // var listUrlCategory = doc.DocumentNode.SelectNodes("//div[@class='nav_main']//ul//li//a");
            string[] listUrlCategory = { "/p0c1000/sao.vnn", "/p0c1004/thoi-trang.vnn","/p0c1002/phim.vnn" };
            string[] nameCate = { "Sao","Thời Trang","Điện Ảnh"};
            string[] posCate = {"2","6","8"};
            for (int h = 0; h < listUrlCategory.Count(); h++)
            {
                string itemPage = urlGoc + listUrlCategory[h];
                string name = nameCate[h];
                ListCate.AddRange(load.LoadCatesWithSub(itemPage, "//div[@id='nav_footer']//ul//li",
             "//div[@id='submenu']", urlGoc, manuId, "", "//a[@title='" + name + "']", "//a", posCate[h], true));

            }
                for (int i = 0; i < ListCate.Count()-1; i++)
                {
                    //trang dau
                //    List<Page> PageFirstAndLast = new List<Page>();
                    // lay link dau tien sau do cho duoi chay tu 1-> trang cuoi
                //    PageFirstAndLast = load.LoadPage(ListCate[i], "//div[@class='pager']//a", urlGoc);
                    // 
                //  if(PageFirstAndLast.Count> 0)
                 
                   
                     
                        
                        ListCate[i].Url   = ListCate[i].Url + "/trang-1.vnn/";
                        //     p.Url = PageFirstAndLast[0].Url.Replace("-1", "-" + j);
                        // featured news
                      
                            List<NewsItemModel> ListFeaturedNews = new List<NewsItemModel>();
                            var loadFeatured1 = load.LoadUpdateNewHomeNews(ListCate[i],"//div[@id='tleft']//div[@id='nbm']", "//h1//a", "", "//h1//a", urlGoc);
                            var loadFeatured2 = load.LoadUpdateNewHomeNews(ListCate[i], "//ul[@id='t3news']//li", "//h2//a", "", "//h2//a", urlGoc);
                            ListFeaturedNews.AddRange(loadFeatured1);
                            ListFeaturedNews.AddRange(loadFeatured2);

                            for (int k = 0; k < ListFeaturedNews.Count(); k++)
                            {
                                var item = load.LoadNews(ListFeaturedNews[k], "//div[@id='VietAd']", "//h2[@class='detail_sapo']", manuId,0, "//div[@id='VietAd']//img", urlGoc,true);
                                if (item != null)
                                {
                                    dem++;
                                    item.Hot = "Tin hot";
                                    load.AddItemToListView(item);
                                    pdb.SaveImgFeatured(folder, item);
                                }

                                //if (dem == 30)
                                //    break;
                            }
                        
                        // load tin khac phia duoi 
                        List<NewsItemModel> ListDetail = new List<NewsItemModel>();
                        var load1 = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@id='listNews']//div//div", "//h2//a", "//p", "//h2//a", urlGoc);



                        ListDetail.AddRange(load1);

                        for (int k = 0; k < ListDetail.Count(); k++)
                        {
                            var item = load.LoadNews(ListDetail[k], "//div[@id='VietAd']", "", manuId, 0, "//div[@id='VietAd']//img",urlGoc,false);
                            if (item != null)
                            {
                                dem++;
                                load.AddItemToListView(item);
                                pdb.SaveImgNormal(folder, item);
                            }

                            //if (dem == 30)
                            //    break;
                        }

                     


                    





                
            }
            //    ListCate = load.LoadCatesNoSub(urlGoc, "//ul[@class='tabs']//li[@class='i-menu']", urlGoc, manuId);
    




          



        //    System.Windows.Forms.MessageBox.Show("\nThanh cong!Co " + dem + " tin moi");

            updateBtn.UpdateButtonStartAfterRun(btnStart);
            updateBtn.UpdateButtonStopAfterRun(btnStop);
            //updateBtn.UpdateAfterRun(btnStart,btnStop);
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");






        }
    }
}
